# -*- coding: utf-8 -*-

"""
flask 参数以及web相关的参数配置
"""
import ast
import os
import traceback
from copy import deepcopy

import yaml

import zs_config
from air_web.config.dws_agg_conf import dws_agg_conf_dic, industry_agg_conf_dic

running_cfg = zs_config.get_running_cfg()
config = running_cfg.get("WEB_DWS_CONF", {})
dws_agg_conf = dws_agg_conf_dic
dws_agg_conf["save_es"] = config.get("save_es", True)
industry_agg_conf = industry_agg_conf_dic

basedir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))


class BaseConfig:
    SECRET_KEY = "\xa7e-\xb0\xe0\x85\x87\x9f\xd1\x92b\xdc"


class DevelopmentConfig(BaseConfig):
    DEBUG = True
    ENV = "development"


class TestingConfig(BaseConfig):
    TESTING = True


class ProductionConfig(BaseConfig):
    DEBUG = False
    ENV = "production"


app_conf_dict = {
    "development": DevelopmentConfig,
    "testing": TestingConfig,
    "production": ProductionConfig,
}

app_conf = app_conf_dict[config["web_mode"]]


def get_ip_list_from_os(key):
    ip_list = os.getenv(key)
    try:
        if ip_list:
            return ast.literal_eval(ip_list)
        return [None]
    except Exception as e:
        traceback.print_exc(e)
        return [None]


def get_os_config():
    os_cfg = {
        "ES_HOST": get_ip_list_from_os("ES_IP_LIST")[0],
        "ES_PORT": os.getenv("ES_PORT"),
        "ES_USER": os.getenv("ES_ROOT_USER"),
        "ES_PASSWORD": os.getenv("ES_ROOT_PASSWORD"),
        "MYSQL_HOST": get_ip_list_from_os("MYSQL_IP_LIST")[0],
        "MYSQL_PORT": os.getenv("MYSQL_PORT"),
        "MYSQL_USER": os.getenv("MYSQL_USER"),
        "MYSQL_PASSWORD": os.getenv("MYSQL_PASSWORD"),
        "MYSQL_ENCODING": os.getenv("MYSQL_ENCODING", "utf8"),
        "REDIS_HOST": get_ip_list_from_os("REDIS_IP_LIST")[0],
        "REDIS_PORT": os.getenv("REDIS_PORT"),
        "REDIS_PASSWORD": os.getenv("REDIS_PASSWORD"),
    }
    for k, v in deepcopy(os_cfg).items():
        if v is None:
            os_cfg.pop(k, None)
            continue
        if k == "ES_HOST":
            os_cfg[
                "ES_HOST"
            ] = f"{os_cfg['ES_USER']}:{os_cfg['ES_PASSWORD']}@{os_cfg['ES_HOST']}:{os_cfg['ES_PORT']}"
    return os_cfg


def get_connect_config():
    connect_yaml_conf = running_cfg.get("connect", {})
    con_cfg = {
        "ES_HOST": connect_yaml_conf["es"]["hosts"],
        "ES_PORT": connect_yaml_conf["es"]["port"],
        "ES_USER": connect_yaml_conf["es"]["root_user"],
        "ES_PASSWORD": connect_yaml_conf["es"]["root_password"],
        "MYSQL_HOST": connect_yaml_conf["mysql"]["hosts"][0],
        "MYSQL_PORT": connect_yaml_conf["mysql"]["port"],
        "MYSQL_USER": connect_yaml_conf["mysql"]["root_user"],
        "MYSQL_PASSWORD": connect_yaml_conf["mysql"]["root_password"],
        "REDIS_HOST": connect_yaml_conf["redis"]["hosts"][0],
        "REDIS_PORT": connect_yaml_conf["redis"]["port"],
        "REDIS_USER": connect_yaml_conf["redis"]["user"],
        "REDIS_PASSWORD": connect_yaml_conf["redis"]["password"],
        "KAFKA_HOST": connect_yaml_conf["kafka"]["hosts"],
        "KAFKA_PORT": connect_yaml_conf["kafka"]["port"],
        "KAFKA_USER": connect_yaml_conf["kafka"]["user"],
        "KAFKA_PASSWORD": connect_yaml_conf["kafka"]["password"],
    }
    for k, v in deepcopy(con_cfg).items():
        if v is None:
            con_cfg.pop(k, None)
            continue
        if k == "ES_HOST":
            con_cfg["ES_HOST"] = [
                f"{con_cfg['ES_USER']}:{con_cfg['ES_PASSWORD']}@{i}:{con_cfg['ES_PORT']}"
                for i in con_cfg["ES_HOST"]
            ]
        if k == "KAFKA_HOST":
            con_cfg["KAFKA_HOST"] = [
                f"{i}:{con_cfg['KAFKA_PORT']}" for i in con_cfg["KAFKA_HOST"]
            ]
    return con_cfg


config.update(get_connect_config())
print(config)
print(dws_agg_conf)
